package test;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;

import jm.JMC;
import jm.music.data.Note;
import jm.music.data.Part;
import jm.music.data.Phrase;
import jm.music.data.Score;
import jm.music.tools.Mod;
import jm.util.Play;

public class MyUtils implements JMC{
	/*
	 * 使用乐器发声，演示用，固定发声 " Do Re Mi"
	 * @param musicalInstrumentName  乐器名称，可以是“PIANO"，”VIOLIN"或“GUITAR"
	 */
	public static void play123with(String musicalInstrumentName) {
		try {
			System.setOut(new PrintStream("tmpfile"));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		
		int[] pitches = {C4,D4,E4};//节奏
		double[] rhythms ={0.75, 0.75, 1};//节奏
		
		List<Note> noteList = new ArrayList<>();
		for(int i=0; i<pitches.length; i++) {
			noteList.add(new Note(pitches[i], rhythms[i]));
		}
		Phrase phrase1 = new Phrase("乐节1");
		phrase1.addNoteList(noteList.toArray(new Note[0]));
		Mod.repeat(phrase1,1);
		
		Part part1 = null;
		switch(musicalInstrumentName) {
			case "PIANO": 
				part1 = new Part("声部1",PIANO,0);
				break;
			case "VIOLIN": 
				part1 = new Part("声部1",VIOLIN,0);
				break;
			case "GUITAR": 
				part1 = new Part("声部1",GUITAR,0);
				break;
			default:
				part1 = new Part("声部1",PIANO,0);
		}
		part1.addPhrase(phrase1);
		
		Score score = new Score("我的谱曲");
		score.add(part1);
		
		Play.midi(score);
	}
}
